#{extends 'main.html'/}
<script type="text/javascript">
   //user,dept初始值,放在最外面，让所有方法都能读取
    var groupicon = "/public/ligerUI/skins/icons/communication.gif";
    $(function () {
        //var tag=${tag};

        var grid ,v_grid;
        var detailWin = null, curentData = null, currentIsAddNew;
	    var mainform = $("#mainform");
	    
	    var v_detailWin = null,projectTypeRow=null;
	    
	    var v_detailForm = $("#visualizeForm");
        
        function f_initGrid()
        { 
            grid = $("#maingrid").ligerGrid({
                columns: [
                    { display: 'id', name: 'id', width: 50,  hide: true},
                    { display: '名称', name: 'name', width: 220, type: 'string', align:'left'},
                    { display: '备注', name: 'remark', width: 220, type: 'string', align:'left'}
                ],
                frozen:false,
                url: "/projectType/json",
				method: "GET",                
                //detail: { onShowDetail: f_showNote,onExtend:f_showNote,height:'auto' },
                title:"工程类型数据维护",
                width: '90%', 
                pageSize: 30,
                height:425,
				rownumbers:false,
				detail: {
                    onShowDetail: f_showVisualize,
                    onCollapse:f_hiddenVisualize,
                    onExtend:f_visualizeUnfold,
                    height:310,width:400
                },
                toolbar: { items: [
                        { text: '添加', click: addRow, icon: 'add' },
                        { line: true },
                        { text: '修改', click: modifyRow, icon: 'modify' },
                        { line: true },
                        { text: '删除', click: deleteRow, icon: 'delete' }
                    ] }
            });
                 
        }
        
        //初始化工程表格
        f_initGrid();
        

      
        //添加工程
        function addRow( )
        {  
			showDetail({},false);
        }
        //添加工程初始化 表单
        function showDetail(data,isAddNew){

            currentData = data;
            currentIsAddNew = isAddNew;
            if (detailWin)
            {
                detailWin.show(); 
            }
            else
            {
                //创建表单结构
	            mainform.ligerForm({
	                
	                fields: [
	           			{ name: "projectType.id", type: "hidden" },
	           			{ display: "名称", name: "projectType.name", newline: true, 
	           			  labelWidth: 100, width: 220, space: 30, type: "text", 
	           			  validate: { required: true, maxlength: 50} },
	           			{ display: "备注", name: "projectType.remark", newline: true, 
	                        labelWidth: 100, width: 220, space: 30, type: 'text', type: 'textarea',
	                        validate:{required:false,maxlength:200}}
	                                                       
	                ],       			
	                    toJSON: JSON2.stringify
	                });            

                detailWin = $.ligerDialog.open({
                    target: $("#detail"),
                    width: 450, height:300, top:80,
                    buttons: [
                    { text: '确定', onclick: function () { $("#mainform").submit(); } },
                    { text: '取消', onclick: function () { detailWin.hide(); } }
                    ]
                });
            }
            
            if (currentData)
            {   
            	$("[name$=id]").val(currentData.id);
            	$("[name$=name]").val(currentData.name);
                $("[name$=remark]").val(currentData.remark);            
                $("[name$=name]").focus();
            }
            
            formSumit({
        		  formId : "mainform",
        		  url :'/projectType/add',
        		  grid:grid,
        		  dialog :detailWin,
        		  sumitSuccess :null,
        		  beforeSubmit : null
        	  });

        }
        
        
        //修改工程类型
        function modifyRow()
        {         
            var selected =   grid.getSelected();
            if (!selected) { LG.tip('请选择行!'); return }
            
            showDetail({
                id: selected.id,
                name:selected.name,
                remark:selected.remark
            }, false);         
        }
        
     	//删除工程类型
        function deleteRow()
        {
        	f_delete({
        		selected:grid.getSelected(),
        		grid:grid,
        		url:"/projectTypes/delete?id="
        	});
        }
        
//工程形象
        function f_showVisualize(row, detailPanel){
            $(detailPanel).empty();
            $(detailPanel).children("div").remove("div");
        	projectTypeRow = row;
        	var v_detial = document.createElement('div'); 
        	$(detailPanel).append(v_detial);
        	createVisualizeGrig(v_detial,projectTypeRow);
        }
        //关闭明细
        function f_hiddenVisualize(row ,detailPanel){
            projectTypeRow=null;
            c_grid=null;
        }
        //展开明细
        function f_visualizeUnfold(row ,detailPanel){
        	f_showVisualize(row,detailPanel);
        }
        //创建contract表格
        function createVisualizeGrig(v_detial,row){
            v_grid = $(v_detial).css('margin',5).ligerGrid({
                columns:
                   [
                        { display: 'id', name: 'id',width:5, hide: true},
                        { display: 'projectTypeId', width:5,name: 'projectTypeId', hide: true},
                        { display: '描述', name: 'descriptions',width: 150,type:'string' },
                        { display: '单位', name: 'unitQuantity', width: 100,type:'string' },
                        { display: '备注', name: 'remark',width:120,type:'string'}
                   ], 
                frozen:false,
                isScroll: true,
                showToggleColBtn: true,
                width:400,
                height:200,
                url:"/projectType/loadVisualize?id="+row.id,
                method: "GET", 
                //data:getContract(row.id),
                usePager:false,
                showTitle: false,
                //onAfterShowData: callback,
                frozen:false,
                toolbar: { items: [
                             { text: '添加', click: addRow_v, icon: 'add' },
                             { line: true },
                             { text: '修改', click: modifyRow_v, icon: 'modify' },
                             { line: true },
                             { text: '删除', click: deleteRow_v, icon: 'delete' }
                     ] }
            });  
        }
        
        function addRow_v(){
        	show_v_detail({projectTypeId:projectTypeRow.id},false);
        }

        function modifyRow_v(){
        	 var selected = v_grid.getSelected();
        	 if(selected){
        		 show_v_detail(
   				 {
   					 id:selected.id,
   					 projectTypeId:selected.projectTypeId,
   					 descriptions:selected.descriptions,
   					 unitQuantity:selected.unitQuantity,
   					 remark:selected.remark
   				 },
   				 false);
        	 }else{
        		 LG.tip('请选择行');
        	 }
        }

        function deleteRow_v(){
        	
        	f_delete({
        		selected:v_grid.getSelected(),
        		grid:v_grid,
        		url:"/projectType/deleteVisulize?id="
        	});
        }
        
	   //弹出合同表单（添加/修改）
       function show_v_detail(contractData,isAddNew){

           if(v_detailWin){
               v_detailWin.show(); 
           }else{
	           //创建表单结构
	           v_detailForm.ligerForm({
                
                fields: [
					{ display:"id",name: "projectVisualize.id",id:"projectVisualizeId", type: "hidden" },
           			{ display:"projectTypeId",name: "projectVisualize.projectType.id",id:"projectTypeId", type: "hidden" },
           			{ display: "描述", name: "projectVisualize.descriptions", newline: true, 
           			  labelWidth: 100, width: 220, space: 30, type: "text", 
           			  validate: { required: true, maxlength: 50} },
           			{ display: "单位", name: "projectVisualize.unitQuantity", newline: true, 
                  		labelWidth: 100, width: 220, space: 30, type: "text",
                  		validate: { required: true, maxlength: 10} },
           			{ display: "备注", name: "projectVisualize.remark", newline: true, 
                        labelWidth: 100, width: 220, space: 30, type: 'textarea',
                        validate:{required:false,maxlength:100}}
                                                       
                ],       			
                    toJSON: JSON2.stringify
                });     

               v_detailWin = $.ligerDialog.open({
                   target: $("#visualizeDetail"),
                   width: 400, height:300, top:80,
                   buttons: [
                   { text: '确定', onclick: function () { 
                	     save();	
                	   //$("#contractForm").submit(); 
                	   	} },
                   { text: '取消', onclick: function () { v_detailWin.hide(); } }
                   ]
               });
           }
           
           if(contractData){
        	  	$("[id='projectVisualizeId']").val(contractData.id);
				$("[id='projectTypeId']").val(contractData.projectTypeId);
				$("[name$=descriptions]").val(contractData.descriptions);
				$("[name$=unitQuantity]").val(contractData.unitQuantity);
				$("[name$=remark]").val(contractData.remark);          
				$("[name$=name]").focus();
           };
           
           //加验证使用
           formSumit({
     		  formId : "visualizeForm",
     		  url :'/projectType/addVisulize',
     		  grid:v_grid,
     		  dialog :v_detailWin,
     		  sumitSuccess :null,
     		  beforeSubmit : null
     	  });
          
           
           function save(){
        	   //$("#contractForm").validate();
               
               $.ajax({
                   loading: '正在保存数据中...',
                   type: 'POST',
                   url: '/projectType/addVisulize',                  
                   data: $("#visualizeForm").serialize(),
                   success: function ()
                   {
                	   v_detailWin.hide();
                	   v_grid.loadData(true);
                       LG.tip('保存成功!');
                   },
                   error: function (message)
                   {
                       LG.tip(message);
                   }
               });
           }
       }
		
    })   
</script>

<div id="query">
    <form id = "queryform" method="post" action="/projects/serach"></form>
    <div id="queryButton"></div>
</div>
<div id="maingrid" style="margin-top:10px"></div> <br />
<div style="display:none;"></div>
<div id="detail"   style="display:none;">
    <form id="mainform" column="300" method="post"></form>
</div>

<div id="visualizeDetail" style="display:none">
	<form id="visualizeForm" column="300" method="post"></form>
</div>
